package com.emm.yixun.mobile.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.model.GetHousesList.BuildingList;
import com.emm.yixun.mobile.ui.housing.MarketingControlActivity;

public class HousingMyGridViewAdapter extends BaseAdapter {
	ArrayList<BuildingList> list;
	Context context;
	String premisesName;

	public HousingMyGridViewAdapter(Context context, ArrayList<BuildingList> list, String premisesName) {
		this.context = context;
		this.list = list;
		this.premisesName = premisesName;
	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {
		return list == null ? null : list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHoudlers v = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.housing_mygridview_item, null);
			v = new ViewHoudlers();
			v.bg_item = (RelativeLayout) convertView.findViewById(R.id.bg_item);
			v.content_item = (TextView) convertView.findViewById(R.id.content_item);
			convertView.setTag(v);
		} else {
			v = (ViewHoudlers) convertView.getTag();
		}
		if ("1".equals(list.get(position).getIsSell())) {// 可售
			v.bg_item.setBackgroundResource(R.drawable.select_housing2);
		} else {
			v.bg_item.setBackgroundResource(R.drawable.select_housing1);
		}
		v.content_item.setText(list.get(position).getBuildingName());
		v.bg_item.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.setClass(context, MarketingControlActivity.class);
				intent.putExtra("buildingId", list.get(position).getBuildingId());
				intent.putExtra("premisesId", list.get(position).getPremisesId());
				intent.putExtra("buildingName", list.get(position).getBuildingName());
				intent.putExtra("premisesName", premisesName);
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	public static class ViewHoudlers {

		RelativeLayout bg_item;

		TextView content_item;

	}

}
